

const fs = require("fs");
const stream = require("stream");
const path = require("path");
const util = require("util");
const mongo = require("./mongo");
const dbg = require("debug")("[index.js]"); dbg.enabled = true;
const Collection_Name = "Alibaba" + parseInt(Math.random() * 10000);
dbg(Collection_Name);
let phase = 0;
let buf = "";
let tmpImageType = "";

let store = {
    boundary: "",
    messages: [],
    html: [],
    pics: [{
        type: "image/gif",
        cid: "xxxxdemo",
        content: "swgWGBWRGBEGF"
    }]
};

(async () => {
    let client = await mongo.connect();
    let collection = client.db('qq-mht-parser').collection(Collection_Name);
    let fileReadStream = fs.createReadStream(
        path.join(__dirname, "./Alibaba.mht"),
        {
            encoding: "utf8",
            highWaterMark: 1 * 1024
        }
    );
    let line_process_func = async function (line) {
        dbg(`phase is ${phase}`,`line is ${line}}`);
        return new Promise(async (resolve) => {
            switch (phase) {
                case 0: {
                    //找出boundary

                    if (line.startsWith("\tboundary=")) {
                        let boundary = line.replace("\tboundary=", "").split("\"")[1];
                        store.boundary = boundary;
                        phase = 1;
                        let inserted = await
                            collection.insert({ type: "boundary", value: boundary });
                        return resolve(inserted);
                    }
                    return resolve(null);
                    break;
                }
                case 1: {
                    //准备解析html或者图片
                    if (line.includes(store.boundary)) {
                        phase = 2;
                        return resolve(phase);
                    }
                    return resolve(null);
                    break;
                }
                case 2: {
                    //要解析一些东西，但是不知道具体是要解析html还是图片
                    if (line.startsWith("Content-Type")) {
                        if (line.includes("image/")) {
                            //image
                            tmpImageType = line.replace("Content-Type:", "");
                            phase = 22;
                        } else {
                            //html
                            phase = 21;
                        }
                        return resolve(phase);
                    }
                    return resolve(null);
                    break;
                }
                case 21: {
                    //正在解析html
                    if (line.startsWith("<") && line.endsWith(">")) {
                        let matched = line.match(/<tr><td.*<\/td><\/tr>/g);
                        if (Array.isArray(matched)) {
                            for (let m of matched) {
                                store.html.push(m);
                                await collection.insert({
                                    type: "html",
                                    value: m
                                }).catch(err => {
                                    debugger
                                })
                            }
                        }
                        if (line.includes(`</html>`)) {
                            phase = 1;
                        }
                        return resolve(phase);
                        break;
                    }
                    return resolve(null);

                    break;
                }
                case 22: {
                    //正在解析图片
                    if (line.startsWith("Content")) {
                        if (line.startsWith("Content-Location")) {
                            store.pics.unshift({
                                cid: line.replace("Content-Location:{", "").replace("}.dat", ""),
                                type: tmpImageType,
                                content: ""
                            });
                            let typeCopy = tmpImageType;
                            tmpImageType = "";
                            let inserted = await collection.insert({
                                type: "image",
                                cid: line.replace("Content-Location:{", "").replace("}.dat", ""),
                                image_type: typeCopy,
                                content: ""
                            })
                            return resolve(inserted);
                        } else {
                            return resolve(null);
                        }

                    } else {
                        if (line.includes(store.boundary)) {
                            phase = 2
                            return resolve(phase);
                        } else {
                            store.pics[0].content += `${line}\n`;

                            let image_doc = await collection.findOne({
                                cid: store.pics[0].cid
                            })
                            let content = image_doc['content'];
                            content += `${line}\n`;
                            let updated = await collection.findOneAndUpdate({

                                cid: store.pics[0].cid

                            }, {
                                    content: content
                                }).catch(err => {
                                    debugger
                                })
                            debugger
                            return resolve(updated);
                        }
                    }
                    break;
                }
            }
        })
    }
    fileReadStream.on("data", async (chunk) => {
        let chunk_arr = String(buf + chunk).split("\n");
        buf = "";
        fileReadStream.pause

        for (let i = 0; i < chunk_arr.length;) {
            if (i == chunk_arr.length - 1) {
                buf = chunk_arr[i];
                break;
            }
            let line = chunk_arr[i];
            if (line.length == 0) {
                continue;
            } else {
                let processed = await line_process_func(line);
                dbg(`[processed-${i} ${process}]`)
            }

            i++
        }
        fileReadStream.resume();
        " debugger"
    })

    fileReadStream.on("close", () => {
        store
    })

})();

